package io.paper.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.paper.entity.BeerMoldEntity;
import io.paper.entity.CustomerEntity;
import io.paper.entity.MoonBillEntity;
import io.paper.service.MoonBillService;
import io.renren.controller.AbstractController;
import io.renren.utils.PageUtils;
import io.renren.utils.R;

/**
 * 系统用户
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/bill")
public class MoonBillController extends AbstractController {
	@Autowired
	private MoonBillService moonBillService;
	
	/**
	 * 查询客户月结单
	 */
	@RequestMapping("/list")
	public R queryBilllist(MoonBillEntity param,Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("startRow", (page - 1) * limit+1);
		map.put("endRow", page* limit);
		map.put("para", param);
		//查询列表数据
		List<MoonBillEntity> iqcSupplierBaseInfoTList = moonBillService.queryBilllist(map);
		int total = moonBillService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(iqcSupplierBaseInfoTList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 查询客户月结单
	 */
	@RequestMapping("/queryBilllist")
	public R queryBilllist(MoonBillEntity param){
		Map<String, Object> map = new HashMap<>();
		map.put("para", param);
		return R.ok().put("list", moonBillService.queryBilllist(map));
	}
	
	/**
	 * 导出月结单
	 */
	@RequestMapping("/exportMoonBillExcel")
	public void exportMoonBillExcel(MoonBillEntity param,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("para", param);
		String starttime = param.getStartDate();
		String endtime = param.getEndDate();
		String cUSTOMERID = param.getcUSTOMERID();
		logger.info(cUSTOMERID+"导出月结单："+map.toString());
		OutputStream out = null;
		boolean isExport = false;
		try {
			out = response.getOutputStream();// 取得输出流
			//设定输出文件头
			response.setContentType("application/vnd.ms-excel");// 定义输出类型
			response.setHeader("Content-Disposition","attachment;filename="+new String((cUSTOMERID+"月账单"+starttime+"至"+endtime).getBytes("gbk"),"iso8859-1")+".xls");
			isExport = moonBillService.exportMoonBillExcel(map, out);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(out!=null){
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
		logger.info("导出月结单："+isExport);
	}
	

	
	
	/**
	 * 查询客户列表
	 */
	@RequestMapping("/queryCustomerlist")
	public R queryCustomerlist(CustomerEntity param){
		Map<String, Object> map = new HashMap<>();
		map.put("para", param);
		return R.ok().put("list", moonBillService.queryCustomerlist(map));
	}
}
